package com.example.dynamicplan;

import java.util.ArrayList;
import java.util.List;

public class ChangeExchange1 {
    public int ChangeExchange(int[] coins, int amt) {
        int[][] dp = new int[coins.length][amt + 1];
        for (int i = 0; i < coins.length; i++) {
            dp[i][0] = 0;
        }
        for (int j = 1; j < amt + 1; j++) {
            dp[0][j] = amt + 1;
        }
        for (int i = 1; i < coins.length; i++) {
            for (int j = 1; j < amt + 1; j++) {
                if (j < coins[i - 1]) {
                    dp[i][j] = dp[i - 1][j];
                }
                dp[i][j] = Math.min(dp[i][j - coins[i - 1]] + 1, dp[i - 1][j]);
            }
        }
        if (dp[coins.length - 1][amt] == amt) {
            return -1;
        } else {
            return dp[coins.length - 1][amt];
        }
    }

    public int ChangeExchangeCount(int[] coins, int amt) {
        int[][] dp = new int[coins.length][amt + 1];
        for (int i = 0; i < coins.length; i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i < coins.length; i++) {
            for (int j = 1; j < amt + 1; j++) {
                if (j < coins[i - 1]) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]];
                }
            }
        }
        return dp[coins.length][amt];
    }
}
